import type { ShapeType } from '@/interface';
import { DEFAULTLAYER } from '@/constant';

interface PropsType {
  startX?: number;
  startY?: number;
}

class BasicLine implements ShapeType {
  private type: string;
  private layer: number;
  startX: number;
  startY: number;
  ends: number[];
  constructor(props?: PropsType) {
    this.type = 'basicline';
    this.layer = DEFAULTLAYER;
    this.startX = props?.startX || 0;
    this.startY = props?.startY || 0;
    this.ends = [this.startX, this.startY];
  }

  getType(): string {
    return this.type;
  }

  getLayer(): number {
    return this.layer;
  }

  setEnd(x: number, y: number): void {
    this.ends.push(x);
    this.ends.push(y);
  }

  draw(ctx: CanvasRenderingContext2D): void {
    ctx.beginPath();
    ctx.moveTo(this.startX, this.startY);
    for (let index = 0; index < this.ends.length - 1; index += 2) {
      const x = this.ends[index];
      const y = this.ends[index + 1];
      ctx.lineTo(x, y);
    }
    ctx.stroke();
  }
}

export default BasicLine;
